<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class community extends Admin_Controller{
    function __construct() {
        parent::__construct();
        $this->load->model('bbs_article_cate_model');
        $this->load->model('tree_model');
        $this->load->model('bbs_article_model');
        $this->load->model('bbs_users_model');
        $this->load->model('bbs_user_group_model');
        if(!$this->session->userdata('username')){
            redirect('login');
        }
        $username = $this->session->userdata('username');
    }
    function index(){
        $keyword = trim($_GET['q']);
        $like = $keyword ? $keyword : '';
        $where = "1=1";
        
        if($_GET['status']){
            $status = $_GET['status'] - 1;
            $where .= " and a.status = $status";
        }
        $left_join = "";
        if(isset($_GET['cate_type']) && $_GET['cate_type'] && $_GET['q']){
            $cate_val = trim($_GET['cate_type']);
            $keyword = trim($_GET['q']);
            $cate_name = "";
            switch ($cate_val){
                case "cat":
                    $cate_info = $this->db->query("select id from tan_bbs_article_category where name like '%$keyword%' order by id desc limit 1")->row_array();
                    $cate_info['id'] = $cate_info ? $cate_info['id'] :  0;
                    $cate_name = "文章栏目";
                    $where .= " and a.cat_id = $cate_info[id]";
                    break;
                case "title":
                    $cate_name = "文章标题";
                    $where .= " and a.title like '%$keyword%'";
                    break;
                case "tag":
                    $cate_name = "文章标签";
                    $where .= " and a.tags_name like '%$keyword%'";
                    break;
                case "name":
                    $cate_name = "用户名";
                    $where .= " and u2.username = '$keyword'";
                    break;
                case "nick":
                    $cate_name = "用户昵称";
                    $where .= " and i.nickname like '%$keyword%'";
                    break;
            }
            $this->template->assign('cat',$cate_val);
            $this->template->assign('cate_name',$cate_name);
            $this->template->assign('keyword',$keyword);
        }
        //排序
        $order = isset($_GET['order']) ? trim($_GET['order']) : "";
        $this->template->assign('order_name',$order);
        switch ($order){
            case "hot":
                $hot_order = $_GET['hot_order'];
                $order_by = "a.hot $hot_order,a.sort desc,a.id desc";
                $this->template->assign('hot_order_val',$hot_order == "desc" ? "asc" : "desc");
                break;
            case "recommend":
                $rec_order = $_GET['rec_order'];
                $order_by = "a.recommend $rec_order,a.sort desc,a.id desc";
                $this->template->assign('rec_order_val',$rec_order == "desc" ? "asc" : "desc");
                break;
            case "show":
                $show_order = $_GET['show_order'];
                $order_by = "a.show $show_order,a.sort desc,a.id desc";
                $this->template->assign('show_order_val',$show_order == "desc" ? "asc" : "desc");
                break;
            case "sort":
                $sort_order = $_GET['sort_order'];
                $order_by = "a.sort $sort_order,a.id desc";
                $this->template->assign('sort_order_val',$sort_order == "desc" ? "asc" : "desc");
                break;
            case "readnum":
                $read_order = $_GET['read_order'];
                $order_by = "a.readnum $read_order,a.id desc";
                $this->template->assign('read_order_val',$read_order == "desc" ? "asc" : "desc");
                break;
            case "commentnum":
                $comment_order = $_GET['comment_order'];
                $order_by = "a.commentnum $comment_order,a.id desc";
                $this->template->assign('comment_order_val',$comment_order == "desc" ? "asc" : "desc");
                break;
            case "likes":
                $likes_order = $_GET['likes_order'];
                $order_by = "a.likes $likes_order,a.id desc";
                $this->template->assign('likes_order_val',$likes_order == "desc" ? "asc" : "desc");
                break;
            default :
                $order_by = "a.id desc";
                break;
        }
        //分页
        $getcount = $this->db->query("select a.* from tan_bbs_article a left join tan_users_info i on i.user_id = a.authorid left join tan_users u2 on u2.user_id = a.authorid where $where order by a.id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->db->query("select a.title,a.id,a.authorid,a.cat_id,a.readnum,a.commentnum,a.likes,a.status,a.create_time,a.hot,a.recommend,a.sort,a.show,i.nickname,c.name cat_name from tan_bbs_article a left join tan_users_info i "
                . "on i.user_id = a.authorid left join tan_bbs_article_category c on c.id = a.cat_id left join tan_users u2 on u2.user_id = a.authorid where $where order by $order_by limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page,$like)->result_array();
        foreach ($list as $key=>$val){
            $list[$key]["create_time"] = date("Y-m-d H:i:s",$val["create_time"]);
            //$group = $this->bbs_user_group_model->get(array("id"=>$val['group_id']));
            $list[$key]['major'] = $group['group_name'];
            $list[$key]['space_url'] = $this->www_base_url("u/".encrypt_user_id($val['authorid']));
            if($val['status'] == 1){
                $list[$key]['detail_url'] = $this->www_base_url("p/".encrypt_article_id($val['id']));
            }else{
                $list[$key]['detail_url'] = $this->www_base_url("p/upub/".encrypt_article_id($val['id']));
            }
        }
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        //重定义页面
        $this->template->assign('Cate',$_GET['cate']);
        $this->template->assign('Pid', $_GET['cate_p']);
        $this->template->assign('segment_p','index');
        $this->template->assign('Status', $_GET['status']);
        $this->template->assign('cate_name',$cate_name);
        $this->template->assign('pid_name', $pid_name);
        $this->template->load('community/index');
    }
    //前台菜单管理
    function menu(){
        $listtree = $this->bbs_article_cate_model->getCategoryList(0);
        
        foreach($listtree as $k=>$val)
        {
            $listtree[$k]['son'] = $this->bbs_article_cate_model->getCategoryList($val['id']);
        }
        //重定义页面
        $this->template->assign('listtree',$listtree);
        $this->template->assign('segment_p','menu');
        $this->template->load('community/menu');
    }
    //添加系统菜单
    function menu_add(){
        if($_POST){
            $mssage = array();
            $title	= trim($_POST['name']);
            $sort	= trim($_POST['sort']);
            $pid	= trim($_POST['pid']);
            $group	= trim($_POST['group']);
            $hide	= trim($_POST['is_show']);
            $icon       = trim($_POST['icon']);
            if(!$title){
                $mssage['info']	  = '名称必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['pid']	= $pid;
            $data['name']	= $title;
            $data['sort']	= $sort;
            $data['group']	= $group;
            $data['is_show']	= $hide;
            $data['icon']       = $icon;
            $result = $this->bbs_article_cate_model->insert($data);
            if($result){
                update_cache('forummenu');
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/menu');
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/menu');
            }
            echo json_encode($mssage);exit;
        }
        $cate_id = $this->input->get('pid')? $this->input->get('pid') : 0;
        $this->template->assign('cate_id',$cate_id);
        $station=$this->bbs_article_cate_model->tree($this->bbs_article_cate_model->GetCategoryAll());
        $this->template->assign('station',$station);
        $this->template->assign('segment_p','menu');
        $this->template->load('community/menu_add');
    }
    //菜单快速编辑
    function menu_edit()
    {	
        if($_POST){
            $mssage = array();
            
            $id	   = trim($_POST['id']);
            $name = trim($_POST['name']);
            $sort  = trim($_POST['sort']);
            $code   = trim($_POST['code']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }
            if(!$name){
                    $mssage['info']	  = '名称必须填写';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data['name'] = $name;
            $data['code'] = $code;
            $data['sort'] = $sort;//
            $result = $this->bbs_article_cate_model->update($data,$id);
            if($result){
                update_cache('forummenu');
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/menu');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/menu');
                echo json_encode($mssage);exit;
            }
        }

    }
    
    
    
    //修改系统菜单
     function menu_medit(){
         if($_POST){
            $mssage = array();
            $id		= trim($_POST['id']);
            $title	= trim($_POST['name']);
            $sort	= trim($_POST['sort']);
            $pid	= trim($_POST['pid']);
            $icon       = trim($_POST['icon']);
            $group	= trim($_POST['group']);
            $hide	= trim($_POST['is_show']);
            $tip	= trim($_POST['description']);
            if(!$title){
                $mssage['info']	  = '名称必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['pid']	= $pid;
            $data['name']	= $title;
            $data['sort']	= $sort;
            $data['icon']       = $icon;
            $data['group']	= $group;
            $data['is_show']	= $hide;
            $data['description']	= $tip;
            $result = $this->bbs_article_cate_model->update($data,$id);
            if($result){
                //更新菜单缓存
                update_cache('forummenu');
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/menu');
            }else{
                $mssage['info']	  = '修改不成功';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/menu');
            }
            echo json_encode($mssage);exit;
        }
         //------------------
        $id = $_GET['id'];
        $item = $this->bbs_article_cate_model->get_category_data($id);
	$station=$this->bbs_article_cate_model->tree($this->bbs_article_cate_model->GetCategoryAll());
        $this->template->assign('station',$station);
        $this->template->assign('item',$item);
        //重定义页面
        $this->template->assign('segment_p','menu');
        $this->template->load('community/menu_edit');
     }
     //是否隐藏菜单
    function menu_setstatus()
    {	
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['ids']);
            $status = trim($_GET['status']);
            $data = array();
            $data['is_show'] = $status;
            $result = $this->bbs_article_cate_model->update($data,$id);
            if($result){
                update_cache('forummenu');
                if($status==1){
                    $mssage['info']	  = '禁用成功';
                }else{
                    $mssage['info']	  = '启用成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }	

    }
    //设置文章分类推荐
    function menu_setrecommend(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['ids']);
            $recommend = trim($_GET['recommend']);
            $data = array();
            $data['recommend'] = $recommend;
            $result = $this->bbs_article_cate_model->update($data,$id);
            if($result){
                update_cache('forummenu');
                if($recommend==1){
                    $mssage['info']	  = '推荐成功';
                }else{
                    $mssage['info']	  = '取消成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    
    public function menu_remove()
	{
            $id	   = intval($_GET['id']);
            if(!$id){
                redirect('community/menu');
            }
            
            $this->db->where('id', $id);
            $this->db->delete('bbs_article_category');
            if($this->db->affected_rows()){
                    update_cache('forummenu');
                    $mssage['info']	  = '删除成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('community/menu');
                    echo json_encode($mssage);exit;
           }else{
                    $mssage['info']	  = '删除失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('community/menu');
                    echo json_encode($mssage);exit;
           }
	
	}
    public function setstatus()
    {	
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $status = trim($_GET['status']);
            $type = trim($_GET['type']);
            if($type==""){
            $result = $this->bbs_article_model->setstatus($status,$id);
            }else{
            $result = $this->bbs_article_model->status($status,$id,$type);    
            }
            if($result){
                $this->cache_hot_article();
                 $mssage['info']	  = '操作成功';
                 $mssage['status'] = 1;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
            }else{
                 $mssage['info']	  = '操作失败';
                 $mssage['status'] = 0;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
            }
        }	
    }
    
    //设置加精
    function setdigest(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $status = trim($_GET['status']);
            $result = $this->bbs_article_model->status($status,$id,"digest");
            if($result){
                //TODO发送站内信
                
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                 $mssage['info']	  = '操作失败';
                 $mssage['status'] = 0;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
            }
        }	
    }
    
     public function setstar()
    {
       if($_GET){
            $message = array();
            $id = trim($_GET['id']);
            $star = trim($_GET['star']);
            if($star == 2){
                $star = $star+1;
            }
            if($star == -2){
                $star = 1;
            }
            $result = $this->bbs_users_model->setstar($star,$id);
            if($result){
                    if($star==1){
                        $mssage['info']	  = '启用成功';
                        }else{
                        $mssage['info']	  = '禁用成功';
                        }
                        $mssage['status'] = 1;
                        $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                        echo json_encode($mssage);exit;
           }else{
                    $mssage['info']	  = '操作失败';
                        $mssage['status'] = 0;
                        $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                        echo json_encode($mssage);exit;
           }
        }
    }
        function remove(){
            	$id = intval($_GET['id']);
		if(!$id){
		redirect('community/index');
		}
		
			   $result = $this->bbs_article_model->delete($id);
			    if($this->db->affected_rows()){
                                
				    $mssage['info']	  = '删除成功';
                                    $mssage['status'] = 1;
                                    $mssage['url']	  = site_url('community/index');
                                    echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '删除失败';
                                    $mssage['status'] = 0;
                                    $mssage['url']	  = site_url('community/index');
                                    echo json_encode($mssage);exit;
			   }
        }
        function articles_edit(){
                $id  = intval($_GET['id']);
		if(!$id){
		redirect('community/index');
		}
		if($_POST){
			$mssage = array();
			$id	= intval($_POST['id']);
                        
			if(!$id){
			redirect('community/index');
			}
                        
                        $article = $this->db->select("is_notice,authorid")->from("bbs_article")->where(array("id"=>$id))->order_by("id desc")->get()->row_array();
                        
			$cat_id	= trim($_POST['cat_id']);
			$title		= trim($_POST['title']);
			if(trim($_REQUEST['thumb'])){
			$thumb = trim($_REQUEST['thumb']);
			}
			$content	= trim($_POST['content']);
			$summary	= trim($_POST['summary']);
			$status		= trim($_POST['status']);
			
			if(!$cat_id){
				$mssage['info']	  = '请选择分类';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			if(!$title){
				$mssage['info']	  = '页面名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
	
			$data = array();
			$data['cat_id'] = $cat_id;
			$data['title'] = $title;
			if(!empty($thumb)){
			$data['thumb'] = $thumb;
			}else{
			}
			$data['content'] = $content;
			$data['summary'] = $summary;
			$data['status'] = $status;
			$result = $this->bbs_article_model->update($data,$id);
			   if($result){
                               
                               $this->cache_hot_article();
                               
                               //发送消息系统通知给关注用户
                               if(!$article['is_notice']){
                                   
                                   $this->db->insert("bbs_notice",array('user_id'=>0,"recieve_id"=>$article['authorid'],'operator_id'=>$id,"event_type"=>'event_add_article',"type"=>"system","is_show"=>1,"create_time"=>time()));
                                   $this->db->insert("bbs_notice",array('user_id'=>$article['authorid'],'operator_id'=>$id,"event_type"=>'event_add_article',"type"=>"article","is_show"=>1,"create_time"=>time()));
                                   //获取所以关注作者的用户
                                   $followers = $this->db->select("followuid")->from("bbs_fans")->where(array("user_id"=>$article["authorid"],"is_del"=>1))->order_by("id desc")->get()->result_array();
                                   if($followers){
                                        $datas = array();
                                        foreach ($followers as $key=>$val){
                                            $data = array();
                                            $data['user_id'] = 0;
                                            $data['recieve_id'] = $val['followuid'];
                                            $data['operator_id'] = $id;
                                            $data['event_type'] = "event_add_article_care";
                                            $data['create_time'] = time();
                                            $data['is_show'] = 1;
                                            $data['type'] = "article";
                                            $datas[] = $data;
                                        }
                                        
//                                        $datas[] = array('user_id'=>0,'recieve_id'=>$article['authorid'],'operator_id'=>$id,"event_type"=>'event_add_article',"type"=>"article","is_show"=>0,"create_time"=>time());
                                        
                                        $this->db->insert_batch("bbs_notice",$datas);
                                   }
                                   
                               }
//                                update_cache('article');
				   $mssage['info']	  = '添加成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('community/index');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '修改失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('community/index');
					echo json_encode($mssage);exit;
			   }

		}
		
		$item = $this->bbs_article_model->Get($id);
		$catelist = $this->bbs_article_cate_model->get_content_List();
		$catelist = $this->bbs_article_cate_model->tree($catelist);
		$this->template->assign('catelist',$catelist);
		//重定义页面
		$this->template->assign('segment_p','index');
		$this->template->assign('item',$item);
		$this->template->load('community/articles_edit');
        }
        function articles_add()
        {
            		if($_POST){
			$mssage = array();
			
			$cat_id	= trim($_POST['cat_id']);
			$title		= trim($_POST['title']);
                        $summary        = trim($_POST['summary']);
			$thumb		= trim($_POST['thumb']);
			$content	= trim($_POST['content']);
			$status		= trim($_POST['status']);
			if(!$cat_id){
				$mssage['info']	  = '请选择分类';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			if(!$title){
				$mssage['info']	  = '页面名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			$data = array();
			$data['cat_id'] = $cat_id;
			$data['title'] = $title;
                        $data['summary'] = $summary;
			$data['thumb'] = $thumb;
			$data['content'] = $content;
			$data['status'] = $status;
			$data['authorid'] = $this->session->userdata('user_id');
			$data['create_time'] = time();
			$result = $this->bbs_article_model->insert($data);
			   if($result){
					//update_cache('menu');
				   $mssage['info']	  = '添加成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('community/index');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '修改失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('community/index');
					echo json_encode($mssage);exit;
			   }
	   
			
			
			
		}
		$catelist = $this->bbs_article_cate_model->get_content_List();
		$catelist = $this->bbs_article_cate_model->tree($catelist);
		//print_r($catelist[0]);
		$this->template->assign('catelist',$catelist);
                $this->template->assign('segment_p','index');
                $this->template->load('community/articles_add');
        }
        //帖子阅读量
        function articles_readnum(){
            if($_POST){
                $mssage = array();

                $id    = trim($_POST['id']);
                $readnum  = trim($_POST['readnum']);
                if(!$id){
                        $mssage['info']	  = '系统错误';
                        $mssage['status'] = 0;
                        $mssage['url']	  = '';
                        echo json_encode($mssage);exit;
                }

                $data = array();
                $data['readnum'] = $readnum;
                $result = $this->bbs_article_model->update($data,$id);
                if($result){
                    $mssage['info']	  = '修改成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('community/index');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '修改失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('community/index');
                    echo json_encode($mssage);exit;
                }
            }
        }
        //帖子点赞量
        function articles_likes(){
            if($_POST){
                $mssage = array();

                $id    = trim($_POST['id']);
                $likes  = trim($_POST['likes']);
                if(!$id){
                        $mssage['info']	  = '系统错误';
                        $mssage['status'] = 0;
                        $mssage['url']	  = '';
                        echo json_encode($mssage);exit;
                }

                $data = array();
                $data['likes'] = $likes;
                $result = $this->bbs_article_model->update($data,$id);
                if($result){
                    $mssage['info']	  = '修改成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('community/index');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '修改失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('community/index');
                    echo json_encode($mssage);exit;
                }
            }
        }
        //帖子置顶顺序
        function articles_sort(){
            if($_POST){
                $mssage = array();

                $id    = trim($_POST['id']);
                $sort  = trim($_POST['sort']);
                if(!$id){
                        $mssage['info']	  = '系统错误';
                        $mssage['status'] = 0;
                        $mssage['url']	  = '';
                        echo json_encode($mssage);exit;
                }

                $data = array();
                $data['sort'] = $sort;
                $result = $this->bbs_article_model->update($data,$id);
                if($result){
                    $this->cache_hot_article();
                    $mssage['info']	  = '修改成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('community/index');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '修改失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('community/index');
                    echo json_encode($mssage);exit;
                }
            }
        }
        
        function member(){
            $where = array();
            if($_GET['star']){
                $where['bbs_account.star'] =  $_GET['star'];
            }
            
            if($_GET['st'] && $_GET['q']){
                $keyword = trim($_GET['q']);
                $type = trim($_GET['st']);
                if($type == 1){
                    if(preg_match('/^\d+$/i', $keyword)){
                        $user = $this->bbs_users_model->get_user_by_uid($keyword);
                    }else{
                        $user = $this->bbs_users_model->get_user_by_username($keyword);
                    }
                    $where['bbs_account.user_id'] = $user['user_id'];
                    $icon = '用户名/ID';
                }elseif($type == 2){//手机号
                    $icon = '手机号';
                    $like = $keyword;
                }
            }
                //总数
                $total = $this->bbs_users_model->get_bbs_count($where,$like);
                $navigation_position = 'outside';
                $records_per_page = 10;
                $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,array('left', 'right')) ? $navigation_position : 'outside');
                //$this->xpage->reverse(true);
                $this->xpage->records($total);
                $this->xpage->records_per_page($records_per_page);
                $this->xpage->labels("<<", ">>");
                //记录
                $list = $this->bbs_users_model->get_bbs_list($where, (($this->xpage->get_page() - 1) * $records_per_page),$records_per_page,$like);
                foreach ($list as $key=>$val){
                    $list[$key]['level'] = $val['level']."级";
                    $group = $this->bbs_user_group_model->get(array("id"=>$val['group_id']));
                    $list[$key]['major'] = $group['group_name'];
                    $list[$key]['space_url'] = $this->www_base_url("u/".encrypt_user_id($val['user_id']));
                }
                $this->template->assign('keyword', $_GET['q']);
                $this->template->assign('icon', $icon);
                $this->template->assign('icon_type', $type);
                $this->template->assign('Total', $total);
                $this->template->assign('_star', $_GET['star']);
                $this->template->assign('showpage', $this->xpage->render(true));
                $this->template->assign('list', $list);
            
                //重定义页面
                $this->template->assign('segment_p','member');
                $this->template->load('community/member');
        }
        //用户账户信息
        function account(){
                        $where = array();
            if($_GET['star']){
                $where['bbs_account.star'] =  $_GET['star'];
            }
            
            if($_GET['st'] && $_GET['q']){
                $keyword = trim($_GET['q']);
                $type = trim($_GET['st']);
                if($type == 1){
                    if(preg_match('/^\d+$/i', $keyword)){
                        $user = $this->bbs_users_model->get_user_by_uid($keyword);
                    }else{
                        $user = $this->bbs_users_model->get_user_by_username($keyword);
                    }
                    $where['bbs_account.user_id'] = $user['user_id'];
                    $icon = '用户名/ID';
                }elseif($type == 2){//手机号
                    $where['users_info.nickname'] = $keyword;
                    $icon = '昵称';
                }
            }
                //总数
                $total = $this->bbs_users_model->get_bbs_count($where,$like);
                $navigation_position = 'outside';
                $records_per_page = 10;
                $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,array('left', 'right')) ? $navigation_position : 'outside');
                //$this->xpage->reverse(true);
                $this->xpage->records($total);
                $this->xpage->records_per_page($records_per_page);
                $this->xpage->labels("<<", ">>");
                //记录
                $list = $this->bbs_users_model->get_bbs_list($where, (($this->xpage->get_page() - 1) * $records_per_page),$records_per_page,$like);
                foreach ($list as $key=>$val){
                    $list[$key]['level'] = $val['level']."级";
                    $group = $this->bbs_user_group_model->get(array("id"=>$val['group_id']));
                    $list[$key]['major'] = $group['group_name'];
                }
                $this->template->assign('keyword', $_GET['q']);
                $this->template->assign('icon', $icon);
                $this->template->assign('icon_type', $type);
                $this->template->assign('Total', $total);
                $this->template->assign('_star', $_GET['star']);
                $this->template->assign('showpage', $this->xpage->render(true));
                $this->template->assign('list', $list);
            
                //重定义页面
                $this->template->assign('segment_p','account');
                $this->template->load('community/account');
        }
        //用户信息修改
        function member_edit(){
                $user_id  = intval($_GET['user_id']);
		if(!$user_id){
		redirect('community/member');
		}
		if($_POST){
			$mssage = array();
			$user_id	= intval($_POST['user_id']);
			if(!$user_id){
			redirect('community/member');
			}
			$group_id	= trim($_POST['group_id']);
			
			if(!$group_id){
				$mssage['info']	  = '请选择用户组';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
	
			$data = array();
			$data['group_id'] = $group_id;
			$result = $this->bbs_users_model->update($data,$user_id);
			   if($result){
				   $mssage['info']	  = '添加成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('community/member');
					echo json_encode($mssage);exit;
			   }else{
                                $mssage['info']	  = '修改失败';
                                    $mssage['status'] = 0;
                                    $mssage['url']	  = site_url('community/member');
                                    echo json_encode($mssage);exit;
			   }

		}
		
		$item = $this->bbs_users_model->Get($user_id);
		$grouplist = $this->bbs_users_model->get_group_List();
		$this->template->assign('grouplist',$grouplist);
                //重定义页面
                $this->template->assign('segment_p','member');
                $this->template->assign('item',$item);
                $this->template->load('community/member_edit');
        }
        //单个用户分配权限
        function member_permission(){
            if($_POST){
                $mssage = array();
                $ids = $_POST['rules'];
                $no_access = implode(',',$ids ? $ids : array());
                $data = array();
                $user_id	= intval($_POST['user_id']);

                $data['no_access'] = $no_access;
                $result = $this->bbs_users_model->update($data,$user_id);
                if($result){
                    $mssage['info']	  = '授权成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('community/member');
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '授权失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
                }
            }
            $user_id = $_GET["user_id"];
            $info = $this->bbs_users_model->get_user_info($user_id);
            $item = $this->bbs_users_model->Get($user_id);
            $item['nickname'] = $info['nickname'];
            $item['no_access'] = explode(',',$item['no_access']);
            $station=$this->bbs_user_group_model->tree($this->bbs_user_group_model->GetPermissionAll());
            $this->template->assign('station',$station);
            $this->template->assign('user_id',$user_id);
            $this->template->assign('item',$item);
            $this->template->assign('segment_p','member');
            $this->template->load('community/member_permission');
        }
        function report(){
                $total = $this->db->query("select * from tan_bbs_report_list")->num_rows();
                $navigation_position = 'outside';
                $records_per_page = 10;
                $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,array('left', 'right')) ? $navigation_position : 'outside');
                //$this->xpage->reverse(true);
                $this->xpage->records($total);
                $this->xpage->records_per_page($records_per_page);
                $this->xpage->labels("<<", ">>");
                //记录
                $list = $this->db->query("select * from tan_bbs_report_list order by create_time desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
                foreach ($list as $key=>$val){
                    $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                }
                $this->template->assign('Total', $total);
                $this->template->assign('showpage', $this->xpage->render(true));
                $this->template->assign('list', $list);
                //重定义页面
                $this->template->assign('segment_p','report');
                $this->template->load('community/report');
        }
        
    //用户角色管理
    function group(){
        $list = $this->bbs_user_group_model->get_list(array(),0,10);
        $this->template->assign('list', $list);
        $this->template->assign('segment_p','group');
        $this->template->load('community/group');
    }
    
    //用户组编辑
    function group_edit(){
        if($_POST){
            $id = trim($_POST['id']);
            $group_name = trim($_POST['group_name']);
            $description = trim($_POST['description']);
            $data = array();
            $data['group_name'] = $group_name;
            $data['description'] = $description;
            $result = $this->bbs_user_group_model->update($data,$id);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/group');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/group');
                echo json_encode($mssage);exit;
            }
        }
        $id = $_GET['id'];
        $item = $this->bbs_user_group_model->get(array("id"=>$id));
        $this->template->assign('item', $item);
        $this->template->assign('segment_p','group');
        $this->template->load('community/group_edit');
    }
    
    //用户组编辑
    function group_add(){
        if($_POST){
            $group_name = trim($_POST['group_name']);
            $description = trim($_POST['description']);
            $data = array();
            $data['group_name'] = $group_name;
            $data['description'] = $description;
            $result = $this->bbs_user_group_model->insert($data);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/group');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/group');
                echo json_encode($mssage);exit;
            }
        }
        $this->template->assign('segment_p','group');
        $this->template->load('community/group_add');
    }
    
    //权限管理
    function permission(){
        $listtree = $this->bbs_user_group_model->getpermissionlist(0);
        foreach($listtree as $k=>$val)
        {
            $listtree[$k]['son'] = $this->bbs_user_group_model->getpermissionlist($val['id']);
        }
        $this->template->assign('listtree',$listtree);
        $this->template->assign('segment_p','permission');
        $this->template->load('community/permission');
    }
    
    //添加权限
    function permission_add(){
        if($_POST){
            $mssage = array();
            $name	= trim($_POST['name']);
            $pid	= trim($_POST['pid']);
            $code	= trim($_POST['code']);
            $desc	= trim($_POST['desc']);
            if(!$code){
                    $mssage['info']	  = '标识必须填写';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }
            $data = array();
            $data['pid']	= $pid;
            $data['name']	= $name;
            $data['code']	= $code;
            $data['desc']	= $desc;
            $result = $this->bbs_user_group_model->permission_insert($data);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/permission');
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/permission');
            }
            echo json_encode($mssage);exit;
        }
        $pid = $_GET['pid']? $_GET['pid'] : 0;
        $this->template->assign('f_pid',$pid);
        $station=$this->bbs_user_group_model->tree($this->bbs_user_group_model->GetPermissionAll());
	$this->template->assign('station',$station);
        $this->template->assign('segment_p','permission');
        $this->template->load('community/permission_add');
    }
    //修改权限
    function permission_medit()
    {	
        if($_POST){
            $mssage = array();
            $id		= trim($_POST['id']);
            $name	= trim($_POST['name']);
            $pid	= trim($_POST['pid']);
            $code	= trim($_POST['code']);
            $desc	= trim($_POST['desc']);
            if(!$code){
                $mssage['info']	  = '标识必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['pid']	= $pid;
            $data['name']	= $name;
            $data['code']	= $code;
            $data['desc']	= $desc;
            $result = $this->bbs_user_group_model->permission_update($data,$id);

            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/permission');
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/permission');
            }
            echo json_encode($mssage);exit;
        }
        //------------------
        $id = $_GET['id'];
        $item = $this->bbs_user_group_model->get_permission(array("id"=>$id));
        $station=$this->bbs_user_group_model->tree($this->bbs_user_group_model->GetPermissionAll());
        $this->template->assign('station',$station);
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','permission');
        $this->template->load('community/permission_edit');
    }
    public function permission_edit()
    {	
        if($_POST){
            $mssage = array();
            $id         = trim($_POST['id']);
            $name       = trim($_POST['name']);
            $code	= trim($_POST['code']);
            if(!$code){
                $mssage['info']	  = '标识必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['name'] = $name;
            $data['code'] = $code;
            $result = $this->bbs_user_group_model->permission_update($data,$id);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
        }

    }
    //给权限用户组分配访问权限
    function permission_access(){
        if($_POST){
            $mssage = array();
            $ids = $_POST['rules'];
            $no_access = implode(',',$ids ? $ids : array());
            $data = array();
            $id	= intval($_POST['id']);
            
            $data['no_access'] = $no_access;
            $result = $this->bbs_user_group_model->update($data,$id);
            if($result){
                $mssage['info']	  = '授权成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/group');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '授权失败';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
				
	}
        $id = intval($_GET['id']);
        $item = $this->bbs_user_group_model->get(array("id"=>$id));
        $item['no_access'] = explode(',',$item['no_access']);
        $this->template->assign('item',$item);
        $station=$this->bbs_user_group_model->tree($this->bbs_user_group_model->GetPermissionAll());
        $this->template->assign('station',$station);
        $this->template->assign('segment_p','group');
        $this->template->load('community/permission_access');
    }
    //系统给用户发送站内信
    function member_letter(){
        $user_id  = intval($_GET['user_id']);
        if(!$user_id){
        redirect('community/member');
        }
        if($_POST){
                $mssage = array();
                $user_id	= intval($_POST['user_id']);
                if(!$user_id){
                redirect('community/member');
                }
                $title	= trim($_POST['title']);
                $content = trim($_POST['content']);

                $data = array();
                $data['title'] = $title;
                $data['message'] = $content;
                $data['user_id'] = $user_id;
                $data['from'] = 1;
                $data['create_time'] = time();
                $result = $this->bbs_users_model->add_letter($data);
                   if($result){
                        $mssage['info']	  = '添加成功';
                        $mssage['status'] = 1;
                        $mssage['url']	  = site_url('community/member');
                        echo json_encode($mssage);exit;
                   }else{
                        $mssage['info']	  = '修改失败';
                        $mssage['status'] = 0;
                        $mssage['url']	  = site_url('community/member');
                        echo json_encode($mssage);exit;
                   }
        }
                $item = $this->bbs_users_model->Get($user_id);
        //重定义页面
                $this->template->assign('item',$item);
                $this->template->assign('segment_p','member');
                $this->template->load('community/member_letter');
    }
    //用户举报
    function member_report(){
        $user_id  = intval($_GET['user_id']);
        $id =  intval($_GET['id']);
        if(!$user_id){
        redirect('community/report');
        }
        if($_POST){
                $mssage = array();
                $user_id	= intval($_POST['user_id']);
                if(!$user_id){
                redirect('community/report');
                }
                $id = trim($_POST['id']);
                $opresult = trim($_POST['opresult']);
                $content = trim($_POST['content']);

                $data = array();
                $data['title'] = "举报处理通知";
                $data['message'] = $content;
                $data['user_id'] = $user_id;
                $data['from'] = 1;
                $data['create_time'] = time();
                $opresult = $this->bbs_users_model->opresult($opresult,$id,$user_id);
                $result = $this->bbs_users_model->add_letter($data);
                   if($result){
                        $mssage['info']	  = '添加成功';
                        $mssage['status'] = 1;
                        $mssage['url']	  = site_url('community/report');
                        echo json_encode($mssage);exit;
                   }else{
                        $mssage['info']	  = '修改失败';
                        $mssage['status'] = 0;
                        $mssage['url']	  = site_url('community/report');
                        echo json_encode($mssage);exit;
                   }
        }
                $item = $this->bbs_users_model->Getreport($id);
        //重定义页面
                $this->template->assign('item',$item);
                $this->template->assign('segment_p','report');
                $this->template->load('community/member_report');
    }
    //评论管理
    function comment(){
                $where = array();
                if($_GET['st'] && $_GET['q']){
                    $keyword = trim($_GET['q']);
                    $type = trim($_GET['st']);
                    if($type == 1){
                        $where['bbs_article_comment.id'] = $keyword;
                        $icon = 'ID';
                    }elseif($type == 2){//手机号
                        $like = $keyword;
                        $icon = '标题';
                    }
                }
                //分页
                $getcount = $this->bbs_article_model->comment_count($where,$like);
                $navigation_position = 'outside';
                $records_per_page = 10;
                $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
                //$this->xpage->reverse(true);
                $this->xpage->records($getcount);
                $this->xpage->records_per_page($records_per_page);
                $this->xpage->labels("<<", ">>");
                $list = $this->bbs_article_model->commentList($where,(($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page,$like);
                foreach ($list as $key=>$val){
                    $list[$key]["create_time"] = date("Y-m-d H:i",$val["create_time"]);
                    $list[$key]["content"] = str_replace(">",'<；',$list[$key]["content"]); 
                    $list[$key]["content"] = str_replace(">",'>；',$list[$key]["content"]); 
                    $list[$key]["content"] = str_replace("\n",'>；br/>；',$list[$key]["content"]); 
                    $list[$key]["content"] = preg_replace("[\[em_([0-9]*)\]]","<img src='/static/forum/img/face/$1.gif' />",$list[$key]["content"]); 
                }
                $this->template->assign('Total',$getcount);
                $this->template->assign('showpage',$this->xpage->render(true));
                $this->template->assign('list',$list);
                $this->template->assign('icon', $icon);
                $this->template->assign('icon_type', $type);
        //重定义页面
                $this->template->assign('segment_p','comment');
                $this->template->load('community/comment');
    }
    //评论显示设置
    function commshow()
    {	
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $status = trim($_GET['status']);
            $data = array();
            $data['is_show'] = $status;
            $result = $this->bbs_article_model->commshow($data,$id);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }	

    }
    //文章标签列表
    function arttags(){
        $keyword = trim($_GET['q']);
        $like = $keyword ? $keyword : '';
        $where = array();
        //分页
        $getcount = $this->bbs_article_model->getarttagsCount($where,$like);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->bbs_article_model->getarttagsList($where,(($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page,$like);
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        //重定义页面
        $this->template->assign('segment_p','arttags');
        $this->template->load('community/arttags');
    }
    //设置标签状态
    public function settagstatus(){	
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $status = trim($_GET['status']);
            $type = trim($_GET['type']);
            $from = trim($_GET['from']);
            if($from == "art"){
            $result = $this->bbs_article_model->setarttag($status,$id,$type);
            }
            if($from == "wenda"){
            $result = $this->bbs_article_model->setwdtag($status,$id,$type);
            }
            if($result){
                 $mssage['info']	  = '操作成功';
                 $mssage['status'] = 1;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
            }else{
                 $mssage['info']	  = '操作失败';
                 $mssage['status'] = 0;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
            }
        }	
    }
    //添加文章标签
     function arttags_add(){
            		if($_POST){
			$mssage = array();
			
			$name		= trim($_POST['name']);
                        $letter_first        = trim($_POST['letter_first']);
			$letter_all		= trim($_POST['letter_all']);

			if(!$name){
				$mssage['info']	  = '标签名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
                        if(!$letter_first){
				$mssage['info']	  = '标签首字母必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
                        if(!$letter_all){
				$mssage['info']	  = '标签字母全拼必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			$data = array();
			$data['name'] = $name;
                        $data['letter_first'] = $letter_first;
			$data['letter_all'] = $letter_all;
  
			$result = $this->bbs_article_model->insertarttag($data);
      
			if($result){
					//update_cache('menu');
				   $mssage['info']	  = '添加成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('community/arttags');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '添加失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('community/arttags');
					echo json_encode($mssage);exit;
			   }
			
		}
                $this->template->assign('segment_p','arttags');
                $this->template->load('community/arttags_add');
        }
        //修改文章标签
               function arttags_edit(){
                $id  = intval($_GET['id']);
		if(!$id){
		redirect('community/arttags');
		}
		if($_POST){
			$mssage = array();
			$id	= intval($_POST['id']);
			if(!$id){
			redirect('community/index');
			}
			$name	        = trim($_POST['name']);
			$letter_first		= trim($_POST['letter_first']);
                        $letter_all		= trim($_POST['letter_all']);
                            
			if(!$name){
				$mssage['info']	  = '标签名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			if(!$letter_first){
				$mssage['info']	  = '标签首字母必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
                        if(!$letter_all){
				$mssage['info']	  = '标签字母全拼必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
	
			$data = array();
			$data['name'] = $name;
			$data['letter_first'] = $letter_first;
			$data['letter_all'] = $letter_all;			
			$result = $this->bbs_article_model->updatearttag($data,$id);
			   if($result){
				   $mssage['info']	  = '添加成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('community/arttags');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '修改失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('community/arttags');
					echo json_encode($mssage);exit;
			   }

		}
		
		$item = $this->bbs_article_model->Getarttag($id);
		//重定义页面
		$this->template->assign('segment_p','arttags');
		$this->template->assign('item',$item);
		$this->template->load('community/arttags_edit');
        }
    //问答列表
    function wenda(){
        
        $where = "1=1";
        
        //条件搜索
        $seach_type = isset($_GET['seach_type']) ? trim($_GET['seach_type']) : "";
        $keyword = isset($_GET['q']) ? trim($_GET['q']) : "";
        if($seach_type && $keyword){
            switch ($seach_type){
                case "title":
                    $where .= " and w.title like '%$keyword%'";
                    $seach_type_name = "问答标题";
                    break;
                case "tag":
                    $where .= " and w.tags_name like '%$keyword%'";
                    $seach_type_name = "问答标签";
                    break;
                case "name":
                    $where .= " and u.username = '$keyword'";
                    $seach_type_name = "用户名";
                    break;
                case "nick":
                    $where .= " and i.nickname like '%$keyword%'";
                    $seach_type_name = "用户昵称";
                    break;
            }
            
            $this->template->assign('seach_type',$seach_type);
            $this->template->assign('seach_type_name',$seach_type_name);
            $this->template->assign('keyword',$keyword);
        }
        //排序
        $order = isset($_GET['order']) ? trim($_GET['order']) : "";
        $this->template->assign('order_name',$order);
        switch ($order){
            case "hot":
                $hot_order = $_GET['hot_order'];
                $order_by = "w.hot $hot_order,w.sort desc,w.id desc";
                $this->template->assign('hot_order_val',$hot_order == "desc" ? "asc" : "desc");
                break;
            case "recommend":
                $rec_order = $_GET['rec_order'];
                $order_by = "w.recommend $rec_order,w.sort desc,w.id desc";
                $this->template->assign('rec_order_val',$rec_order == "desc" ? "asc" : "desc");
                break;
            case "show":
                $show_order = $_GET['show_order'];
                $order_by = "w.is_show $show_order,w.sort desc,w.id desc";
                $this->template->assign('show_order_val',$show_order == "desc" ? "asc" : "desc");
                break;
            case "sort":
                $sort_order = $_GET['sort_order'];
                $order_by = "w.sort $sort_order,w.id desc";
                $this->template->assign('sort_order_val',$sort_order == "desc" ? "asc" : "desc");
                break;
            case "readnum":
                $read_order = $_GET['read_order'];
                $order_by = "w.readnum $read_order,w.id desc";
                $this->template->assign('read_order_val',$read_order == "desc" ? "asc" : "desc");
                break;
            case "answernum":
                $answer_order = $_GET['answer_order'];
                $order_by = "w.answernum $answer_order,w.id desc";
                $this->template->assign('answer_order_val',$answer_order == "desc" ? "asc" : "desc");
                break;
            case "likes":
                $likes_order = $_GET['likes_order'];
                $order_by = "w.likes $likes_order,w.id desc";
                $this->template->assign('likes_order_val',$likes_order == "desc" ? "asc" : "desc");
                break;
            case "focusnum":
                $focus_order = $_GET['focus_order'];
                $order_by = "w.focusnum $focus_order,w.id desc";
                $this->template->assign('focus_order_val',$focus_order == "desc" ? "asc" : "desc");
                break;
            default :
                $order_by = "w.id desc";
                break;
        }
        //分页
        $getcount = $this->db->query("select w.* from tan_bbs_wenda w left join tan_users u on u.user_id = w.questioner_id left join tan_users_info i on i.user_id = w.questioner_id where $where order by w.id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->db->query("select w.*,i.nickname from tan_bbs_wenda w left join tan_users u on u.user_id = w.questioner_id left join tan_users_info i on i.user_id = w.questioner_id where $where order by $order_by limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key => $val){
            $list[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $list[$key]['wenda_url'] = $this->www_base_url("wenda/".encrypt_wenda_id($val['id']));
        }
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->assign('segment_p','wenda');
        $this->template->load('community/wenda');
    }
    
    //问答-修改信息
    function wenda_edit(){
        if($_POST){
            $wenda_id = trim($_POST['id']);
            $data = array();
            $data['title'] = trim($_POST["title"]);
            $result = $this->db->where("id",$wenda_id)->update("tan_bbs_wenda",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
        $wenda_id = trim($_GET['id']);
        $item = $this->db->query("select w.* from tan_bbs_wenda w where w.id = $wenda_id order by id desc limit 1")->row_array(); 
        $this->template->assign('segment_p','wenda');
        $this->template->assign('item',$item);
        $this->template->load('community/wenda_edit');
    }
    //问答-设置热门
    function wenda_sethot(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $hot = trim($_GET['hot']);
            $data = array();
            $data['hot'] = $hot;
            $result = $this->db->where(array("id"=>$id))->update("bbs_wenda",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }    
    //问答-设置推荐
    function wenda_setrecommend(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $recommend = trim($_GET['recommend']);
            $data = array();
            $data['recommend'] = $recommend;
            $result = $this->db->where(array("id"=>$id))->update("bbs_wenda",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    //问答-设置显示或隐藏
    function wenda_setshow(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $is_show = trim($_GET['is_show']);
            $data = array();
            $data['is_show'] = $is_show;
            $result = $this->db->where(array("id"=>$id))->update("bbs_wenda",$data);
            if($result){
                if($is_show==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    //问答-设置显示或隐藏
    function wenda_setreadnum(){
        if($_POST){
            $mssage = array();
            $id	   = trim($_POST['id']);
            $readnum = trim($_POST['readnum']);
            $data = array();
            $data['readnum'] = $readnum;
            $result = $this->db->where(array("id"=>$id))->update("bbs_wenda",$data);
            if($result){
                if($is_show==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    //问答-设置排序
    function wenda_setsort(){
        if($_POST){
            $mssage = array();
            $id    = trim($_POST['id']);
            $sort  = trim($_POST['sort']);
            $data = array();
            $data['sort'] = $sort;
            $result = $this->db->where(array("id"=>$id))->update("bbs_wenda",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
     //问答标签列表
    function wdtags(){
        $keyword = trim($_GET['q']);
        $like = $keyword ? $keyword : '';
        $where = array();
        //分页
        $getcount = $this->bbs_article_model->getwdtagsCount($where,$like);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->bbs_article_model->getwdtagsList($where,(($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page,$like);
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        //重定义页面
        $this->template->assign('segment_p','wdtags');
        $this->template->load('community/wdtags');
    }       
    //添加问答标签
     function wdtags_add(){
            		if($_POST){
			$mssage = array();
			
			$name		= trim($_POST['name']);
                        $letter_first        = trim($_POST['letter_first']);
			$letter_all		= trim($_POST['letter_all']);

			if(!$name){
				$mssage['info']	  = '标签名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
                        if(!$letter_first){
				$mssage['info']	  = '标签首字母必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
                        if(!$letter_all){
				$mssage['info']	  = '标签字母全拼必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			$data = array();
			$data['name'] = $name;
                        $data['letter_first'] = $letter_first;
			$data['letter_all'] = $letter_all;
  
			$result = $this->bbs_article_model->insertwdtag($data);
      
			if($result){
					//update_cache('menu');
				   $mssage['info']	  = '添加成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('community/wdtags');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '添加失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('community/wdtags');
					echo json_encode($mssage);exit;
			   }
			
		}
                $this->template->assign('segment_p','wdtags');
                $this->template->load('community/wdtags_add');
        }
        //修改问答标签
               function wdtags_edit(){
                $id  = intval($_GET['id']);
		if(!$id){
		redirect('community/wdtags');
		}
		if($_POST){
			$mssage = array();
			$id	= intval($_POST['id']);
			if(!$id){
			redirect('community/wdtags');
			}
			$name	        = trim($_POST['name']);
			$letter_first		= trim($_POST['letter_first']);
                        $letter_all		= trim($_POST['letter_all']);
                            
			if(!$name){
				$mssage['info']	  = '标签名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			if(!$letter_first){
				$mssage['info']	  = '标签首字母必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
                        if(!$letter_all){
				$mssage['info']	  = '标签字母全拼必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
	
			$data = array();
			$data['name'] = $name;
			$data['letter_first'] = $letter_first;
			$data['letter_all'] = $letter_all;			
			$result = $this->bbs_article_model->updatewdtag($data,$id);
			   if($result){
				   $mssage['info']	  = '添加成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('community/wdtags');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '修改失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('community/wdtags');
					echo json_encode($mssage);exit;
			   }

		}
		
		$item = $this->bbs_article_model->Getwdtag($id);
		//重定义页面
		$this->template->assign('segment_p','wdtags');
		$this->template->assign('item',$item);
		$this->template->load('community/wdtags_edit');
        }    
       //标签排序
        function tag_sort(){
            if($_POST){
                $mssage = array();

                $id    = trim($_POST['id']);
                $sort  = trim($_POST['sort']);
                $from = trim($_POST['from']);
                if(!$id){
                        $mssage['info']	  = '系统错误';
                        $mssage['status'] = 0;
                        $mssage['url']	  = '';
                        echo json_encode($mssage);exit;
                }

                $data = array();
                $data['sort'] = $sort;
                if($from =='art'){
                $result = $this->bbs_article_model->updatearttag($data,$id);
                }
                if($from == 'wenda'){
                $result = $this->bbs_article_model->updatewdtag($data,$id);    
                }
                if($result){
                 $mssage['info']	  = '操作成功';
                 $mssage['status'] = 1;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
                }else{
                 $mssage['info']	  = '操作成功';
                 $mssage['status'] = 0;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
                }
            }
        }

    //问答列表
    function exposure(){

        $where = "1=1 and w.type=1";

        //条件搜索
        $seach_type = isset($_GET['seach_type']) ? trim($_GET['seach_type']) : "";
        $keyword = isset($_GET['q']) ? trim($_GET['q']) : "";
        if($seach_type && $keyword){
            switch ($seach_type){
                case "title":
                    $where .= " and w.title like '%$keyword%'";
                    $seach_type_name = "曝光标题";
                    break;
                case "tag":
                    $where .= " and w.tags_name like '%$keyword%'";
                    $seach_type_name = "曝光标签";
                    break;
                case "name":
                    $where .= " and u.username = '$keyword'";
                    $seach_type_name = "用户名";
                    break;
                case "nick":
                    $where .= " and i.nickname like '%$keyword%'";
                    $seach_type_name = "用户昵称";
                    break;
            }

            $this->template->assign('seach_type',$seach_type);
            $this->template->assign('seach_type_name',$seach_type_name);
            $this->template->assign('keyword',$keyword);
        }
        //排序
        $order = isset($_GET['order']) ? trim($_GET['order']) : "";
        $this->template->assign('order_name',$order);
        switch ($order){
            case "recommend":
                $rec_order = $_GET['rec_order'];
                $order_by = "w.recommend $rec_order,w.sort desc,w.id desc";
                $this->template->assign('rec_order_val',$rec_order == "desc" ? "asc" : "desc");
                break;
            case "show":
                $show_order = $_GET['show_order'];
                $order_by = "w.is_show $show_order,w.sort desc,w.id desc";
                $this->template->assign('show_order_val',$show_order == "desc" ? "asc" : "desc");
                break;
            case "sort":
                $sort_order = $_GET['sort_order'];
                $order_by = "w.sort $sort_order,w.id desc";
                $this->template->assign('sort_order_val',$sort_order == "desc" ? "asc" : "desc");
                break;
            case "readnum":
                $read_order = $_GET['read_order'];
                $order_by = "w.readnum $read_order,w.id desc";
                $this->template->assign('read_order_val',$read_order == "desc" ? "asc" : "desc");
                break;
            case "answernum":
                $answer_order = $_GET['answer_order'];
                $order_by = "w.answernum $answer_order,w.id desc";
                $this->template->assign('answer_order_val',$answer_order == "desc" ? "asc" : "desc");
                break;
            case "likes":
                $likes_order = $_GET['likes_order'];
                $order_by = "w.likes $likes_order,w.id desc";
                $this->template->assign('likes_order_val',$likes_order == "desc" ? "asc" : "desc");
                break;
            case "focusnum":
                $focus_order = $_GET['focus_order'];
                $order_by = "w.focusnum $focus_order,w.id desc";
                $this->template->assign('focus_order_val',$focus_order == "desc" ? "asc" : "desc");
                break;
            default :
                $order_by = "w.id desc";
                break;
        }
        //分页
        $getcount = $this->db->query("select w.* from tan_bbs_exposure w left join tan_users u on u.user_id = w.questioner_id left join tan_users_info i on i.user_id = w.questioner_id where $where order by w.id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->db->query("select w.*,i.nickname from tan_bbs_exposure w left join tan_users u on u.user_id = w.questioner_id left join tan_users_info i on i.user_id = w.questioner_id where $where order by $order_by limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key => $val){
            $list[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $list[$key]['exposure_url'] = $this->www_base_url("exposure/".encrypt_wenda_id($val['id']));
        }
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->assign('segment_p','exposure');
        $this->template->load('community/exposure');
    }
    //曝光--推荐
    function exposure_setrecommend(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $recommend = trim($_GET['recommend']);
            $data = array();
            $data['recommend'] = $recommend;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    //曝光-设置显示或隐藏
    function exposure_setshow(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $is_show = trim($_GET['is_show']);
            $data = array();
            $data['is_show'] = $is_show;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                if($is_show==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    //曝光-设置阅读数
    function exposure_setreadnum(){
        if($_POST){
            $mssage = array();
            $id	   = trim($_POST['id']);
            $readnum = trim($_POST['readnum']);
            $data = array();
            $data['readnum'] = $readnum;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                if($is_show==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    //曝光-设置排序
    function exposure_setsort(){
        if($_POST){
            $mssage = array();
            $id    = trim($_POST['id']);
            $sort  = trim($_POST['sort']);
            $data = array();
            $data['sort'] = $sort;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }


    //问答列表
    function information(){

        $where = "1=1 and w.type=2";

        //条件搜索
        $seach_type = isset($_GET['seach_type']) ? trim($_GET['seach_type']) : "";
        $keyword = isset($_GET['q']) ? trim($_GET['q']) : "";
        if($seach_type && $keyword){
            switch ($seach_type){
                case "title":
                    $where .= " and w.title like '%$keyword%'";
                    $seach_type_name = "资讯标题";
                    break;
                case "tag":
                    $where .= " and w.tags_name like '%$keyword%'";
                    $seach_type_name = "资讯标签";
                    break;
                case "name":
                    $where .= " and u.username = '$keyword'";
                    $seach_type_name = "用户名";
                    break;
                case "nick":
                    $where .= " and i.nickname like '%$keyword%'";
                    $seach_type_name = "用户昵称";
                    break;
            }

            $this->template->assign('seach_type',$seach_type);
            $this->template->assign('seach_type_name',$seach_type_name);
            $this->template->assign('keyword',$keyword);
        }
        //排序
        $order = isset($_GET['order']) ? trim($_GET['order']) : "";
        $this->template->assign('order_name',$order);
        switch ($order){
            case "recommend":
                $rec_order = $_GET['rec_order'];
                $order_by = "w.recommend $rec_order,w.sort desc,w.id desc";
                $this->template->assign('rec_order_val',$rec_order == "desc" ? "asc" : "desc");
                break;
            case "show":
                $show_order = $_GET['show_order'];
                $order_by = "w.is_show $show_order,w.sort desc,w.id desc";
                $this->template->assign('show_order_val',$show_order == "desc" ? "asc" : "desc");
                break;
            case "sort":
                $sort_order = $_GET['sort_order'];
                $order_by = "w.sort $sort_order,w.id desc";
                $this->template->assign('sort_order_val',$sort_order == "desc" ? "asc" : "desc");
                break;
            case "readnum":
                $read_order = $_GET['read_order'];
                $order_by = "w.readnum $read_order,w.id desc";
                $this->template->assign('read_order_val',$read_order == "desc" ? "asc" : "desc");
                break;
            case "answernum":
                $answer_order = $_GET['answer_order'];
                $order_by = "w.answernum $answer_order,w.id desc";
                $this->template->assign('answer_order_val',$answer_order == "desc" ? "asc" : "desc");
                break;
            case "likes":
                $likes_order = $_GET['likes_order'];
                $order_by = "w.likes $likes_order,w.id desc";
                $this->template->assign('likes_order_val',$likes_order == "desc" ? "asc" : "desc");
                break;
            case "focusnum":
                $focus_order = $_GET['focus_order'];
                $order_by = "w.focusnum $focus_order,w.id desc";
                $this->template->assign('focus_order_val',$focus_order == "desc" ? "asc" : "desc");
                break;
            default :
                $order_by = "w.id desc";
                break;
        }
        //分页
        $getcount = $this->db->query("select w.* from tan_bbs_exposure w left join tan_users u on u.user_id = w.questioner_id left join tan_users_info i on i.user_id = w.questioner_id where $where order by w.id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->db->query("select w.*,i.nickname from tan_bbs_exposure w left join tan_users u on u.user_id = w.questioner_id left join tan_users_info i on i.user_id = w.questioner_id where $where order by $order_by limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key => $val){
            $list[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $list[$key]['information_url'] = $this->www_base_url("information/".encrypt_wenda_id($val['id']));
        }
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->assign('segment_p','information');
        $this->template->load('community/information');
    }
    //曝光--推荐
    function information_setrecommend(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $recommend = trim($_GET['recommend']);
            $data = array();
            $data['recommend'] = $recommend;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    //曝光-设置显示或隐藏
    function information_setshow(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $is_show = trim($_GET['is_show']);
            $data = array();
            $data['is_show'] = $is_show;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                if($is_show==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    //曝光-设置阅读数
    function information_setreadnum(){
        if($_POST){
            $mssage = array();
            $id	   = trim($_POST['id']);
            $readnum = trim($_POST['readnum']);
            $data = array();
            $data['readnum'] = $readnum;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    //曝光-设置排序
    function information_setsort(){
        if($_POST){
            $mssage = array();
            $id    = trim($_POST['id']);
            $sort  = trim($_POST['sort']);
            $data = array();
            $data['sort'] = $sort;
            $result = $this->db->where(array("id"=>$id))->update("bbs_exposure",$data);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    //添加问答标签
    function information_add(){
        if($_POST){
            $mssage = array();

            $title		= trim($_POST['title']);
            $author		= trim($_POST['author']);
            $summary        = trim($_POST['summary']);
            $thumb		= trim($_POST['thumb']);
            $content	= trim($_POST['content']);

            if(!$title){
                $mssage['info']	  = '资讯辩题必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['type'] = 2;
            $data['status'] = 1;
            $data['title'] = $title;
            $data['author'] = $author;
            $data['summary'] = $summary;
            $data['thumb'] = $thumb;
            $data['content'] = $content;
            $data['questioner_id'] = $this->session->userdata('user_id');
            $data['create_time'] = time();
            $result = $this->bbs_article_model->information_add($data);
            if($result){
                //update_cache('menu');
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/information');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/information');
                echo json_encode($mssage);exit;
            }




        }

        $this->template->assign('segment_p','information');
        $this->template->load('community/information_add');
    }
    //修改问答标签
    function information_edit(){
        $id  = intval($_GET['id']);
        if(!$id){
            redirect('community/information');
        }
        if($_POST){
            $mssage = array();
            $id	= intval($_POST['id']);

            if(!$id){
                redirect('community/information');
            }

            $information = $this->db->select("*")->from("bbs_exposure")->where(array("id"=>$id))->order_by("id desc")->get()->row_array();

            $title		= trim($_POST['title']);
            $author		= trim($_POST['author']);
            if(trim($_REQUEST['thumb'])){
                $thumb = trim($_REQUEST['thumb']);
            }
            $content	= trim($_POST['content']);
            $summary	= trim($_POST['summary']);

            if(!$title){
                $mssage['info']	  = '资讯名称必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            $data = array();
            $data['title'] = $title;
            $data['author'] = $author;
            if(!empty($thumb)){
                $data['thumb'] = $thumb;
            }else{
            }
            $data['content'] = $content;
            $data['status'] = 1;
            $data['summary'] = $summary;
            $result = $this->bbs_article_model->information_edit($data,$id);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/information');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/information');
                echo json_encode($mssage);exit;
            }

        }

        $item = $this->bbs_article_model->Getinformation($id);
        $this->template->assign('segment_p','information');
        $this->template->assign('item',$item);
        $this->template->load('community/information_edit');
    }

    //问答标签列表
    function exptags(){
        $keyword = trim($_GET['q']);
        $like = $keyword ? $keyword : '';
        $where = array();
        //分页
        $getcount = $this->bbs_article_model->getexptagsCount($where,$like);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->bbs_article_model->getexptagsList($where,(($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page,$like);
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        //重定义页面
        $this->template->assign('segment_p','exptags');
        $this->template->load('community/exptags');
    }
    //设置标签状态
    public function setexptagstatus(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $status = trim($_GET['status']);
            $type = trim($_GET['type']);

            $result = $this->bbs_article_model->setexptag($status,$id,$type);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    //添加问答标签
    function exptags_add(){
        if($_POST){
            $mssage = array();

            $name		= trim($_POST['name']);
            $letter_first        = trim($_POST['letter_first']);
            $letter_all		= trim($_POST['letter_all']);

            if(!$name){
                $mssage['info']	  = '标签名称必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$letter_first){
                $mssage['info']	  = '标签首字母必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$letter_all){
                $mssage['info']	  = '标签字母全拼必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['name'] = $name;
            $data['letter_first'] = $letter_first;
            $data['letter_all'] = $letter_all;

            $result = $this->bbs_article_model->insertexptag($data);

            if($result){
                //update_cache('menu');
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/exptags');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/exptags');
                echo json_encode($mssage);exit;
            }

        }
        $this->template->assign('segment_p','exptags');
        $this->template->load('community/exptags_add');
    }
    //修改问答标签
    function exptags_edit(){
        $id  = intval($_GET['id']);
        if(!$id){
            redirect('community/exptags');
        }
        if($_POST){
            $mssage = array();
            $id	= intval($_POST['id']);
            if(!$id){
                redirect('community/exptags');
            }
            $name	        = trim($_POST['name']);
            $letter_first		= trim($_POST['letter_first']);
            $letter_all		= trim($_POST['letter_all']);

            if(!$name){
                $mssage['info']	  = '标签名称必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$letter_first){
                $mssage['info']	  = '标签首字母必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$letter_all){
                $mssage['info']	  = '标签字母全拼必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            $data = array();
            $data['name'] = $name;
            $data['letter_first'] = $letter_first;
            $data['letter_all'] = $letter_all;
            $result = $this->bbs_article_model->updateexptag($data,$id);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('community/exptags');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('community/exptags');
                echo json_encode($mssage);exit;
            }

        }

        $item = $this->bbs_article_model->Getexptag($id);
        //重定义页面
        $this->template->assign('segment_p','exptags');
        $this->template->assign('item',$item);
        $this->template->load('community/exptags_edit');
    }
    //标签排序
    function exptags_sort(){
        if($_POST){
            $mssage = array();

            $id    = trim($_POST['id']);
            $sort  = trim($_POST['sort']);
            if(!$id){
                $mssage['info']	  = '系统错误';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            $data = array();
            $data['sort'] = $sort;


            $result = $this->bbs_article_model->updateexptag($data,$id);

            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    private function www_base_url($url){
        return "http://www.tan66.com/".$url.".html";
    }
}

